package com.itheima.appserver.service;

import com.itheima.appserver.exception.BusinessException;
import com.itheima.appserver.interceptor.UserHandler;
import com.itheima.autoconfig.template.FaceTemplate;
import com.itheima.autoconfig.template.OssTemplate;
import com.itheima.dubbo.api.UserInfoApi;
import com.itheima.dubbo.api.UserLikeApi;
import com.itheima.model.domain.Counts;
import com.itheima.model.domain.UserInfo;
import com.itheima.model.mongo.UserLike;
import com.itheima.model.vo.ErrorResult;
import com.itheima.model.vo.UserInfoVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@Service
public class UserInfoService {
    @DubboReference
    private UserInfoApi userInfoApi;
    @Autowired
    private OssTemplate ossTemplate;
    @Autowired
    private FaceTemplate faceTemplate;
    @DubboReference
    private UserLikeApi userLikeApi;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private TanhuaService tanhuaService;

    //保存用户
    public void save(UserInfo userInfo) {
        userInfoApi.save(userInfo);
    }

    //设置用户头像
    public void update(MultipartFile headPhoto, Long id) throws IOException {
        //上传照片到阿里云
        String imageUrl = ossTemplate.upload(headPhoto.getOriginalFilename(), headPhoto.getInputStream());
        //通过百度云进行人脸验证
        boolean result = faceTemplate.face(imageUrl);
        //根据验证结果进行对应操作
        if (!result) {
            throw new BusinessException(ErrorResult.faceError());
        } else {
            UserInfo userInfo = new UserInfo();
            userInfo.setId(id);
            userInfo.setAvatar(imageUrl);
            userInfoApi.update(userInfo);
        }

    }

    public UserInfoVo findById(Long userId) {
        UserInfo userInfo = userInfoApi.findById(userId);
        UserInfoVo userInfoVo = new UserInfoVo();
        BeanUtils.copyProperties(userInfo, userInfoVo);
        if (userInfo.getAge() != null) {
            userInfoVo.setAge(userInfo.getAge().toString());
        }
        return userInfoVo;
    }

    public void updateUser(UserInfo userInfo) {
        userInfoApi.update(userInfo);
    }


    //统计
    public Counts coutns() {
        //获取当前用户id
        Long userId = UserHandler.getId();
        //调用api查询当用户被喜欢的人数
        List<Long> ids = userLikeApi.findlove(userId, true);
        //判断
        if (ids == null) {
            return new Counts();
        }
        //设置当前用户喜欢的数量
        Counts counts = new Counts();
        counts.setLoveCount(ids.size());
        //创建互相喜欢的变量
        Integer hxlike = 0;
        for (Long likeUserId : ids) {
            //都是喜欢当前用户的人,都存在redis缓存中,判断反过来的指定keyvalue是否为true即可
//            String key = Constants.USER_LIKE_KEY + likeUserId;
//            Boolean aBoolean = redisTemplate.opsForSet().isMember("key", userId.toString());
//            if (aBoolean) {
//                hxlike++;
//            }
            //有问题,如果在为创建缓存时,数据库中已经有数据了,就取不到缓存
            //查询喜欢人的id等于用户id,被喜欢的用户id等于likeuserid
            UserLike like = userLikeApi.hxlike(likeUserId, userId);
            if (like != null) {
                hxlike++;
            }
        }
        counts.setEachLoveCount(hxlike);
        //设置粉丝数
        Integer fans = userLikeApi.findFan(userId, true);
        counts.setFanCount(fans);
        return counts;
    }
}
